/*
 * for all file's agreement of Maihong
 * wzwz
 * 2014.12.10
 * date        Author       Note
 * 2015.04.13  wzwz         新探针功能
 * 2015.04.22  wzwz         右系统纱嘴左行零位的bug
 * 2015.05.25  wzwz         保存剪夹联动参数
 * 2015.06.17  wzwz         获取度目4距离bug
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <mh/common.h>
#include <mh/mh_knit.h>
#include "func.h"
#include "mhdef.h"

enum {
    MH_FILE_TYPE_MAC_PARAM = 1,
    MH_FILE_TYPE_KNIT_PARAM = 2,
    MH_FILE_TYPE_PARTS_PARAM = 3,
    MH_FILE_TYPE_CONF_PARAM = 4,
    MH_FILE_TYPE_CARRIAGE_STATE = 5,
};

typedef struct{
    uchar m; //'M'
    uchar h; //'H'
    uchar rsvd1;
    uchar rsvd2;
    uint size;
    uchar check[16];
}MH_FILE_HEADER;

/*
 * struct of parts param
 */
struct _MAC_PARTS {
    uchar type;
    uchar rsvd;
    ushort version;
    uint size;//==size of MH_FILE_HEADER
    short select1_to_mac_left_len;
    short select2_to_mac_left_len;
    short select3_to_mac_left_len;
    short select4_to_mac_left_len;

    MOTOR_DRIVE motor_drive;
    ROCK_DRIVE rock_drive;

    uchar select_sect;
    uchar gauze_zero_pulse; //0-needle, 1-pulse
    uchar company;
    uchar gauze_high_voltage;

    uchar select[8];

    struct
    {
        uint cam : 3;
        uint motor : 13;
        /*
         * bit0-度目前1
         * bit1-度目前2
         * bit2-度目后1
         * bit3-度目后2
         * bit4-前辛克
         * bit5-后辛克
         */
        uint gauze : 8;
        uint mac : 6;
        uint rock_compress : 1;
        uint rsvd : 1;
    } sys_info;

    short probe_distance;
    ushort yarn_inter_4cam;

    uchar select_start_sect;
    struct {
        uchar select_array : 1;
        uchar rock_zero_drt : 1;
        uchar sink_drt : 1;
        uchar gauze_type : 1;
        uchar cam_type : 1;
        uchar driver_type : 1;
        uchar rsvd : 2;
    }mac_bit;
    ushort rsvd2;

    short dumu1_to_mac_left_len;
    short dumu2_to_mac_left_len;
    short dumu3_to_mac_left_len;
    short dumu4_to_mac_left_len;

    int rsvd3[100];


};
typedef struct _MAC_PARTS     MAC_PARTS;


#define PARTS_PARAM_FILE          "param/parts.087"
/*
 * read param of parts on machine(not modified by client)
 */
int ReadPartsFile(char *file, struct MH_KNIT *knit_data)
{
    int ret = 0;
    FILE *fp = fopen(file, "r");
    if(!fp)
    {
        return MH_FILE_ERROR_OPEN;
    }
    MH_FILE_HEADER head;
    if(fread(&head, sizeof(head), 1, fp) != 1)
    {
        fclose(fp);
        return MH_FILE_ERROR_READ;
    }
    if(head.m != 'M' || head.h != 'H')
    {
        fclose(fp);
        return MH_FILE_ERROR_HEADER;
    }
    MAC_PARTS data;
    if(fread(&data, __min(sizeof(data), head.size), 1, fp) != 1)
    {
        fclose(fp);
        return MH_FILE_ERROR_DATA;
    }
    fclose(fp);
    if(head.size != data.size)
    {
        return MH_FILE_ERROR_FORMAT;
    }
    SetSelect1DistanceToMacLeft(knit_data, data.select1_to_mac_left_len);
    SetSelect2DistanceToMacLeft(knit_data, data.select2_to_mac_left_len);
    SetSelect3DistanceToMacLeft(knit_data, data.select3_to_mac_left_len);
    SetSelect4DistanceToMacLeft(knit_data, data.select4_to_mac_left_len);
    SetRockDriveData(knit_data, &data.rock_drive);
    SetMotorDriveData(knit_data, &data.motor_drive);
    SetCompany(knit_data, data.company);
    SetGauzeHighVoltage(knit_data, data.gauze_high_voltage);
    SetSelectSect(knit_data, data.select_sect);
    SetSelect(knit_data, data.select);
    SetSysInfo(knit_data, &data.sys_info);
    SetProbeDistance(knit_data, data.probe_distance);
    SetYarnDistanceOfCam(knit_data, data.yarn_inter_4cam);
    SetSelectStartSect(knit_data, data.select_start_sect);
    SetSelectArray(knit_data, data.mac_bit.select_array);
    SetRockZeroDrt(knit_data, data.mac_bit.rock_zero_drt);
    SetSinkDrt(knit_data, data.mac_bit.sink_drt);
    SetDumu1DistanceToMacLeft(knit_data, data.dumu1_to_mac_left_len);
    SetDumu2DistanceToMacLeft(knit_data, data.dumu2_to_mac_left_len);
    SetDumu3DistanceToMacLeft(knit_data, data.dumu3_to_mac_left_len);
    SetDumu4DistanceToMacLeft(knit_data, data.dumu4_to_mac_left_len);
    SetGauzeType(knit_data, data.mac_bit.gauze_type);
    SetCamType(knit_data, data.mac_bit.cam_type);
    SetDriverType(knit_data, data.mac_bit.driver_type);
    //printf("select distance: %d, %d\n", data.select1_to_mac_left_len, data.select2_to_mac_left_len);
    return ret;
}
/*
 *
 */
int WritePartsFile(char *file, struct MH_KNIT *knit_data)
{
    FILE *fp = fopen(file, "w+");
    if (!fp)
    {
        return MH_FILE_ERROR_CREATE;
    }
    MAC_PARTS data;
    memset(&data, 0, sizeof(data));
    MH_FILE_HEADER head;
    memset(&head, 0, sizeof(head));
    head.m = 'M';
    head.h = 'H';
    head.size = sizeof(data);
    if (fwrite(&head, sizeof(head), 1, fp) != 1)
    {
        fclose(fp);
        return MH_FILE_ERROR_WRITE;
    }
    data.type = MH_FILE_TYPE_PARTS_PARAM;
    data.size = sizeof(data);
    data.rsvd = 0;
    data.version = 0;

#if 0//cam == 2
    data.select1_to_mac_left_len = 39;
    data.select2_to_mac_left_len = 158;
    data.select3_to_mac_left_len = 196;
    data.select4_to_mac_left_len = 315;

    data.rock_drive.rock_coder = 2500;
    data.rock_drive.rock_elec_molecular = 32;
    data.rock_drive.rock_elec_denominator = 1;
    data.rock_drive.rock_drive_wheel = 23;//24;
    data.rock_drive.rock_pass_wheel = 48;
    data.rock_drive.rock_pitch = 5;

    data.motor_drive.motor_coder = 2500;
    data.motor_drive.motor_elec_molecular = 14;
    data.motor_drive.motor_elec_denominator = 1;
    data.motor_drive.motor_drive_wheel = 24;
    data.motor_drive.motor_pass_wheel = 68;
    data.motor_drive.motor_belt = 34;
    data.motor_drive.motor_pitch = 8;

    data.select_sect = 8;
    data.gauze_zero_pulse = 0;
    data.company = COMPANY_HQ;
    data.gauze_high_voltage = 40;
    data.select[0] = 7;
    data.select[1] = 6;
    data.select[2] = 5;
    data.select[3] = 4;
    data.select[4] = 3;
    data.select[5] = 2;
    data.select[6] = 1;
    data.select[7] = 0;
    data.sys_info.cam = 2;
    data.sys_info.gauze = 16;
    data.sys_info.motor = 0;
    data.sys_info.mac = 0;
    data.sys_info.rock_compress = TRUE;
    data.probe_distance = -15;
    data.yarn_inter_4cam = 158;

    data.select_start_sect = 8;
    data.mac_bit.select_array = TRUE;
    data.mac_bit.rock_zero_drt = TRUE;
    data.mac_bit.sink_drt = TRUE;

    data.dumu1_to_mac_left_len = 67;
    data.dumu2_to_mac_left_len = 129;
    data.dumu3_to_mac_left_len = 225;
    data.dumu4_to_mac_left_len = 287;
#elif 1
    data.select1_to_mac_left_len = 62;
    data.select2_to_mac_left_len = 189;

    data.rock_drive.rock_coder = 2500;
    data.rock_drive.rock_elec_molecular = 32;
    data.rock_drive.rock_elec_denominator = 1;
    data.rock_drive.rock_drive_wheel = 32;
    data.rock_drive.rock_pass_wheel = 44;
    data.rock_drive.rock_pitch = 5;

    data.motor_drive.motor_coder = 2500;
    data.motor_drive.motor_elec_molecular = 14;
    data.motor_drive.motor_elec_denominator = 1;
    data.motor_drive.motor_drive_wheel = 18;
    data.motor_drive.motor_pass_wheel = 59;
    data.motor_drive.motor_belt = 30;
    data.motor_drive.motor_pitch = 8;
    //data.motor_drive.motor_pitch = 4;
    data.select_sect = 8;
    data.gauze_zero_pulse = 0;
    data.company = COMPANY_RN;
    data.gauze_high_voltage = 40;
    data.select[0] = 7;
    data.select[1] = 6;
    data.select[2] = 5;
    data.select[3] = 4;
    data.select[4] = 3;
    data.select[5] = 2;
    data.select[6] = 1;
    data.select[7] = 0;
    data.sys_info.cam = 1;
    data.sys_info.gauze = 16;
    data.sys_info.motor = 0x29;
    data.sys_info.mac = 1;
    data.sys_info.rock_compress = TRUE;
    data.probe_distance = 20;

    data.select_start_sect = 8;
    data.mac_bit.select_array = TRUE;
    data.mac_bit.rock_zero_drt = TRUE;
    data.mac_bit.sink_drt = TRUE;

    data.dumu1_to_mac_left_len = 64;
    data.dumu2_to_mac_left_len = 190;
    data.dumu3_to_mac_left_len = 0;
    data.dumu4_to_mac_left_len = 0;
#else
    data.select1_to_mac_left_len = 51;
    data.select2_to_mac_left_len = 178;

    data.rock_drive.rock_coder = 2500;
    data.rock_drive.rock_elec_molecular = 32;
    data.rock_drive.rock_elec_denominator = 1;
    data.rock_drive.rock_drive_wheel = 15;//24
    data.rock_drive.rock_pass_wheel = 24;//48
    data.rock_drive.rock_pitch = 5;

    data.motor_drive.motor_coder = 2500;
    data.motor_drive.motor_elec_molecular = 14;
    data.motor_drive.motor_elec_denominator = 1;
    data.motor_drive.motor_drive_wheel = 18;
    data.motor_drive.motor_pass_wheel = 60;
    data.motor_drive.motor_belt = 30;
    data.motor_drive.motor_pitch = 8;
    //data.motor_drive.motor_pitch = 4;
    data.select_sect = 8;
    data.gauze_zero_pulse = 0;
    data.company = COMPANY_RN;
    data.gauze_high_voltage = 40;
    data.select[0] = 0;
    data.select[1] = 1;
    data.select[2] = 2;
    data.select[3] = 3;
    data.select[4] = 4;
    data.select[5] = 5;
    data.select[6] = 6;
    data.select[7] = 7;
    data.sys_info.cam = 1;
    data.sys_info.gauze = 16;
    data.sys_info.motor = 0x29;
    data.sys_info.mac = 0;
    data.sys_info.rock_compress = TRUE;
    data.probe_distance = 20;

    data.select_start_sect = 1;
    data.mac_bit.select_array = FALSE;
    data.mac_bit.rock_zero_drt = TRUE;
    data.mac_bit.sink_drt = TRUE;

    data.dumu1_to_mac_left_len = 46;
    data.dumu2_to_mac_left_len = 183;
    data.dumu3_to_mac_left_len = 0;
    data.dumu4_to_mac_left_len = 0;
#endif

    if (fwrite(&data, sizeof(data), 1, fp) != 1)
    {
        fclose(fp);
        return MH_FILE_ERROR_DATA;
    }
    fclose(fp);
    return MH_FILE_OK;
}
/*
 * struct of mac param
 */
#define MH_FILE_MAC_PARAM_VERSION  0
typedef struct {
    uchar type;
    uchar rsvd;
    ushort version;
    uint size;//==size of MH_FILE_HEADER
    int needle_zero;
    int left_yarn_right_com;
	int left_yarn_left_com;
	int left_forbid;
	int right_forbid;
    float inch_type;
    int select_right_com;
	int select_left_com;
	uchar cam_high_voltage;
	uchar select_high_voltage;
	uchar select_low_voltage;
    int belt_left_com;
	int belt_right_com;
	int belt_pitch_adjust;
    uchar dumu_reset_speed;
	uchar dumu_work_speed;
    ushort total_needle;
    int rock_gap_com;
    uchar rock_speed;
	uchar sink_enable;
	uchar language;
	uchar select_repeat_enable;
	int roll_open_com;           //高罗拉打开修正
    uchar mac_out_enable;
	uchar rock_type;
	uchar rab_percent;
    uchar mac_high_line_speed;
    int back_sink_reset_value;
    uchar mac_low_speed;
    uchar mac_reset_speed;
	uchar mac_mid_speed;
	uchar mac_high_speed;
    int front_sink_reset_value;
    uchar rab_stop_torque;
	uchar mac_ret_len;
    ushort start_needle;
    int yarn_before;
    ushort auto_zero_cloth;
    uchar save_screen_time;
	uchar save_screen_enable;
	int rock_right_tran_com[16];
    uchar shake_enable;
	uchar front_shake_sense;
	uchar back_shake_sense;
    uchar dumu_sprt;                  //度目分次动作
    uint mac_num;
    int rsvd5[7];
    uchar front_safe_door_enable;
	uchar back_safe_door_enable;
    uchar board_enable;
	uchar scissor_enable;
    int rock_right_star_com[16];
	int rock_right_com[16];
	int rock_left_star_com[16];
	int rock_left_com[16];
	uchar rab_relax_for_stop;
    uchar rsvd6;
    uchar rock_mid_speed;
    uchar rock_low_speed;
	int rock_left_tran_com[16];
    uchar yarn_stop[2][8][16];
    ushort sink_value[10][8];
    ushort cam_value[4][8];
    int yarn_stop_com[2][100];
    ushort ext_piece_inter;
	ushort ext_piece_width;
	uchar ext_piece_yarn[10][16];
    uchar ext_piece_enable[10];
    uchar left_feeder_enable;
	uchar right_feeder_enable;
	uchar feeder_speed[32];
	int dmbz_single_com[2][8];
	uchar cam_lock;
	uchar dumu_reset_warn_enable;
	uchar dumu_recv_com;
	uchar dumu_work_delta;
	uchar dumu_reset_delta;
	uchar run_special_enable;
	uchar cam_low_voltage;
	uchar sink_work_speed;
	ushort feeder_start_wait;
	ushort feeder_stop_wait;
	uchar dumu_before_enable;
	uchar second_dumu_enable;
	char second_dumu_com;
	uchar rab_run_type;
	uchar roll_stop_torque;
	uchar knot_wait_line;
	uchar probe_shiled;
	uchar empty_speed;
	uchar yarn_mode_take;
	uchar auto_line_set;
	uchar return_state;
	uchar copy_rab_enable;

	uchar dumu_stop_add;
	uchar hangding_start;
	uchar hangding_end;
	uchar handing_times;
	short mac_before_com[2][32];
	short yarn_before_com[2][2][32];
	int right_yarn_right_com;
	int right_yarn_left_com;

	struct {
	    uint tran_dumu_enable : 1;
	    uint yarn_feeder_mode : 2;
	    uint yarn_take_mode : 1;
	    uint auto_hd_drop : 1;
	    uint rab_enable : 1;
	    uint roll_enable : 1;
	    uint rsvd : 25;
	}knit_state;

	int dmbz_yarn_com[2][8];
	short dumu_before_com[2][32];

	short dumu_max_value;
	uchar ext_piece_num;             //展开片数
	uchar rsvd2;

	ushort rab_min_param;
	ushort rab_max_param;

	int alarm_mask;

	ushort yarn_left_out;
	ushort yarn_right_out;

	int rsvd4[23];

	MAC_PARTS mac_part;

	struct
    {
        int high_pos; //最高位(勾线位)
        int safe_pos; //安全位
        int pos_for_roll_close; //罗拉合上位置
        int hook_open_pos_for_roll; //脱圈位(高罗拉)
        int delay_for_hook_open; //脱圈位滞后
        int speed_for_up_start; //起底板上升初始速度
        int speed_for_up_quick; //起底板上升加速速度
        int speed_for_up_slow; //起底板上升减速速度
        int speed_for_drop_2safe_pos; //降到安全位速度
        int reset_speed; //起底板复位速度
        int pos_for_down_slow; //起底板下降减速位置
        int speed_for_down_slow; //起底板下降减速速度
        int speed_for_roll_when_drop; //落布时罗拉速度
        int wait_for_drop; //落布等待时间
        short board_type;
        short pos_for_up_wait; //起底板上升等待位置
        int hook_open_pos_for_rab; //脱圈位2(主拉布)
        short rab_close_speed; //主拉布合上速度
        short rab_open_speed; //主拉布打开速度
        int pos_for_rab_close; //主拉布合上位置
        short pos_for_up_quick; //起底板上升加速位置
        short pos_for_up_slow; //起底板上升减速位置
        int speed_down_after_hook_open; //起底板下降脱圈速度

        uchar balance; //平衡值
        uchar scissor_times; //剪刀次数
        uchar start_knit_mode;
        uchar gauze;

        uchar pull;
        uchar cs_syn;
        uchar rsvd2;
        uchar rsvd3;
    } board_param;
    struct{
        short select_mode;
        short rock_mode;
        short rock_zero_dir;
        short red_light_time;
        short sever_type;
        short probe_to_mac_left_len;
        short select_start;
        short yarn_type;
        short sink_dir;
        short fuel_gab;
        short fuel_time;
        short select5_to_mac_right_len;
        short select6_to_mac_right_len;
        short mac_width;
        short sink1_to_mac_left_len;
        short sink2_to_mac_left_len;
    }mac_param;    //机器参数界面数据


    short yarn_set[12][100];

}MH_MAC_PARAM;

/*
 * read parameter of machine
 */
int ReadMacFile(char file[], struct MH_KNIT *knit_data)
{
    FILE *fp = fopen(file, "r");
    if(!fp)
    {
        return MH_FILE_ERROR_OPEN;
    }
    MH_FILE_HEADER head;
    if(fread(&head, sizeof(head), 1, fp) != 1)
    {
        fclose(fp);
        return MH_FILE_ERROR_READ;
    }
    if(head.m != 'M'|| head.h != 'H')
    {
        fclose(fp);
        return MH_FILE_ERROR_HEADER;
    }
    MH_MAC_PARAM data;
    if(fread(&data, __min(sizeof(data), head.size), 1, fp) != 1)
    {
        fclose(fp);
        return MH_FILE_ERROR_DATA;
    }
    fclose(fp);
    if(head.size != data.size)
    {
        return MH_FILE_ERROR_FORMAT;
    }
    TurnOnMachine(knit_data, FALSE);
    //mac_parts
    if(ReadPartsFile(PARTS_PARAM_FILE, knit_data))
    {
        SetCompany(knit_data, data.mac_part.company);
        SetSelect1DistanceToMacLeft(knit_data,
            data.mac_part.select1_to_mac_left_len);
        SetSelect2DistanceToMacLeft(knit_data,
            data.mac_part.select2_to_mac_left_len);
        SetSelect3DistanceToMacLeft(knit_data,
            data.mac_part.select3_to_mac_left_len);
        SetSelect4DistanceToMacLeft(knit_data,
            data.mac_part.select4_to_mac_left_len);
        SetRockDriveData(knit_data, &data.mac_part.rock_drive);
        SetMotorDriveData(knit_data, &data.mac_part.motor_drive);
        SetGauzeHighVoltage(knit_data, data.mac_part.gauze_high_voltage);
        SetSelectSect(knit_data, data.mac_part.select_sect);
        SetSelect(knit_data, data.mac_part.select);
        SetSysInfo(knit_data, &data.mac_part.sys_info);
        SetProbeDistance(knit_data, data.mac_part.probe_distance);
        SetYarnDistanceOfCam(knit_data, data.mac_part.yarn_inter_4cam);
        SetSelectStartSect(knit_data, data.mac_part.select_start_sect);
        SetSelectArray(knit_data, data.mac_part.mac_bit.select_array);
        SetRockZeroDrt(knit_data, data.mac_part.mac_bit.rock_zero_drt);
        SetSinkDrt(knit_data, data.mac_part.mac_bit.sink_drt);
        SetGauzeType(knit_data, data.mac_part.mac_bit.gauze_type);
        SetCamType(knit_data, data.mac_part.mac_bit.cam_type);
        SetDriverType(knit_data, data.mac_part.mac_bit.driver_type);
        SetDumu1DistanceToMacLeft(knit_data, data.mac_part.dumu1_to_mac_left_len);
        SetDumu2DistanceToMacLeft(knit_data, data.mac_part.dumu2_to_mac_left_len);
        SetDumu3DistanceToMacLeft(knit_data, data.mac_part.dumu3_to_mac_left_len);
        SetDumu4DistanceToMacLeft(knit_data, data.mac_part.dumu4_to_mac_left_len);
        SetRockCompressEnable(knit_data, TRUE);
    }
    //mac param
    SetNeedleZero(knit_data, data.needle_zero);
    SetLeftYarnRightCom(knit_data, data.left_yarn_right_com);
    SetLeftYarnLeftCom(knit_data, data.left_yarn_left_com);
    SetRightYarnRightCom(knit_data, data.right_yarn_right_com);
    SetRightYarnLeftCom(knit_data, data.right_yarn_left_com);
    SetLeftForbidPos(knit_data, data.left_forbid);
    SetRightForbidPos(knit_data, data.right_forbid);
    SetInchType(knit_data, data.inch_type);
    SetSelectRightCom(knit_data, data.select_right_com);
    SetSelectLeftCom(knit_data, data.select_left_com);
    SetCamHighVoltage(knit_data, data.cam_high_voltage);
    SetCamLowVoltage(knit_data, data.cam_low_voltage);
    SetSelectHighVoltage(knit_data, data.select_high_voltage);
    SetSelectLowVoltage(knit_data, data.select_low_voltage);
    SetMacLeftCom(knit_data, data.belt_left_com);
    SetMacRightCom(knit_data, data.belt_right_com);
    SetBeltPitchAdj(knit_data, data.belt_pitch_adjust);
    SetDumuResetSpeed(knit_data, data.dumu_reset_speed);
    SetDumuWorkSpeed(knit_data, data.dumu_work_speed);
    SetTotalNeedle(knit_data, data.total_needle);
    SetRockGapCom(knit_data, data.rock_gap_com);
    SetRockSpeed(knit_data, data.rock_speed);
    SetSinkEnable(knit_data, data.sink_enable);
    SetLanguage(knit_data, data.language);
    SetSelectRepeatEnable(knit_data, data.select_repeat_enable);
    SetRollOpenCom(knit_data, data.roll_open_com);
    SetMacOutEnable(knit_data, data.mac_out_enable);
    SetRockType(knit_data, data.rock_type);
    SetRabPercent(knit_data, data.rab_percent);
    SetMacHighLineSpeed(knit_data, data.mac_high_line_speed);
    SetBackSinkResetValue(knit_data, data.back_sink_reset_value);
    SetMacLowSpeed(knit_data, data.mac_low_speed);
    SetMacResetSpeed(knit_data, data.mac_reset_speed);
    SetMacMidSpeed(knit_data, data.mac_mid_speed);
    SetMacHighSpeed(knit_data, data.mac_high_speed);
    SetFrontSinkResetValue(knit_data, data.front_sink_reset_value);
    SetRabStopTorque(knit_data, data.rab_stop_torque);
    SetMacRetLen(knit_data, data.mac_ret_len);
    SetStartNeedle(knit_data, data.start_needle);
    SetYarnBefore(knit_data, data.yarn_before);
    SetAutoZeroCloth(knit_data, data.auto_zero_cloth);
    SetSaveScreenTime(knit_data, data.save_screen_time);
    SetSaveScreenEnable(knit_data, data.save_screen_enable);
    SetShakeEnable(knit_data, data.shake_enable);
    SetFrontShakeSense(knit_data, data.front_shake_sense);
    SetBackShakeSense(knit_data, data.back_shake_sense);
    SetMacNum(knit_data, data.mac_num);
    SetFrontSafeDoorEnable(knit_data, data.front_safe_door_enable);
    SetBackSafeDoorEnable(knit_data, data.back_safe_door_enable);
    SetBoardEnable(knit_data, data.board_enable);
    SetScissorEnable(knit_data, data.scissor_enable);
    SetRockMidSpeed(knit_data, data.rock_mid_speed);
    SetRockLowSpeed(knit_data, data.rock_low_speed);
    SetCamLock(knit_data, data.cam_lock);
    SetDumuResetDelta(knit_data, data.dumu_reset_delta);
    SetDumuMaxDelta(knit_data, data.dumu_work_delta);
    SetDumuBeforeEnable(knit_data, data.dumu_before_enable);
    SetDumuRecvCom(knit_data, data.dumu_recv_com);
    SetDumuResetWarnEnable(knit_data, data.dumu_reset_warn_enable);
    SetSinkWorkSpeed(knit_data, data.sink_work_speed);
    SetFeederStartWait(knit_data, data.feeder_start_wait);
    SetFeederStopWait(knit_data, data.feeder_stop_wait);
    SetSpecialRunEnable(knit_data, data.run_special_enable);
    SetRabRunType(knit_data, data.rab_run_type);
    SetRollStopTorque(knit_data, data.roll_stop_torque);
    SetKnotWaitLine(knit_data, data.knot_wait_line);
    SetSecondDumuEnable(knit_data, data.second_dumu_enable);
    SetSecondDumuCom(knit_data, data.second_dumu_com);//二段度目针数
    SetHangDingStart(knit_data, data.hangding_start);
    SetHangDingEnd(knit_data, data.hangding_end);
    SetHangDingTimes(knit_data, data.handing_times);
    SetReturnState(knit_data, data.return_state);
    SetRabCopyEnable(knit_data, data.copy_rab_enable);
    SetYarnTakeMode(knit_data, data.yarn_mode_take);
    SetAutoLineSet(knit_data, data.auto_line_set);
    //0 - 15 needle
    int i;
    for(i = 0; i < 16; ++i)
    {
        SetRockRightStarCom(knit_data, i, data.rock_right_star_com[i]);
        SetRockRightCom(knit_data, i, data.rock_right_com[i]);
        SetRockLeftStarCom(knit_data, i, data.rock_left_star_com[i]);
        SetRockLeftCom(knit_data, i, data.rock_left_com[i]);
        SetRockRightTranCom(knit_data, i, data.rock_right_tran_com[i]);
        SetRockLeftTranCom(knit_data, i, data.rock_left_tran_com[i]);
    }
    SetStopRabRelax(knit_data, data.rab_relax_for_stop);
    int j;
    for(i = 0; i < 2; ++i)
    {
        for(j = 0; j < 8; ++j)
        {
            SetSingleDmbz(knit_data, i, j, data.dmbz_single_com[i][j]);
            SetYarnDmbz(knit_data, i, j, data.dmbz_yarn_com[i][j]);
        }
    }
    //8 sect 16 gauze
    for(i = 0; i < 8; ++i)
    {
        for(j = 0; j < 16; ++j)
        {
            SetRunYarnStop(knit_data, 0, i, j, data.yarn_stop[0][i][j]);
            SetRunYarnStop(knit_data, 1, i, j, data.yarn_stop[1][i][j]);
        }
    }
    //10 sect 8 act
    for(i = 0; i < 10; ++i)
    {
        for(j = 0; j < 8; ++j)
        {
            SetRunSink(knit_data, i, j, data.sink_value[i][j]);
        }
    }
    //4 sect 8 act
    for(i = 0; i < 4; ++i)
    {
        for(j = 0; j < 8; ++j)
        {
            SetCamSettingState(knit_data, i, j, data.cam_value[i][j]);
        }
    }
    //left or right / 20 sect for every 5 speed
    for(i = 0; i < 2; ++i)
    {
        for(j = 0; j < 24; ++j)
        {
            SetYarnStopCom(knit_data, i, j, data.yarn_stop_com[i][j]);
            SetMacBeforeCom(knit_data, i, j, data.mac_before_com[i][j]);
            SetYarnBeforeCom(knit_data, i, j, data.yarn_before_com[i][1][j]);
            SetDumuBeforeCom(knit_data, i, j, data.dumu_before_com[i][j]);
        }
    }
    SetExtPieceNum(knit_data, data.ext_piece_num);
    SetExtPieceInter(knit_data, data.ext_piece_inter);
    SetExtPieceWidth(knit_data, data.ext_piece_width);
    //10 piece(include source) 16 gauze
    for(i = 0; i < 10; ++i)
    {
        for(j = 0; j < 16; ++j)
        {
            SetExtPieceYarn(knit_data, i, j, data.ext_piece_yarn[i][j]);
        }
        SetExtPieceEnable(knit_data, i, data.ext_piece_enable[i]);
    }
    SetLeftYarnFeederEnable(knit_data, data.left_feeder_enable);
    SetRightYarnFeederEnable(knit_data, data.right_feeder_enable);
    //32 sect consistent with mac speed
    for(i = 0; i < 32; ++i)
    {
        SetYarnFeederSpeed(knit_data, i, data.feeder_speed[i]);
    }

    SetProbeShield(knit_data, data.probe_shiled);
    SetTranDumuEnable(knit_data, data.knit_state.tran_dumu_enable);
    SetYarnFeederMode(knit_data, data.knit_state.yarn_feeder_mode);
    SetMacEmptySpeed(knit_data, data.empty_speed);
    SetDumuAddPercent(knit_data, data.dumu_stop_add);
    SetTakeYarnMode(knit_data, data.knit_state.yarn_take_mode);
    SetAutoHdDropEnable(knit_data, data.knit_state.auto_hd_drop);
    SetDumuMaxValue(knit_data, data.dumu_max_value);
    SetRabEnable(knit_data, data.knit_state.rab_enable);
    SetRabMinParam(knit_data, data.rab_min_param);
    SetRabMaxParam(knit_data, data.rab_max_param);
    SetRollEnable(knit_data, data.knit_state.roll_enable);
    SetAlarmMask(knit_data, data.alarm_mask);
    SetDumuSprt(knit_data, data.dumu_sprt);

    SetYarnLeftOut(knit_data, data.yarn_left_out);
    SetYarnRightOut(knit_data, data.yarn_right_out);
    SetBoardParamHighPos(knit_data, data.board_param.high_pos);
    SetBoardParamSafePos(knit_data, data.board_param.safe_pos);
    SetBoardParamRollClosePos(knit_data, data.board_param.pos_for_roll_close);
    SetBoardParamRollHookOpenPos(knit_data, data.board_param.hook_open_pos_for_roll);
    SetBoardParamHookOpenDelay(knit_data, data.board_param.delay_for_hook_open);
    SetBoardParamUpStartSpeed(knit_data, data.board_param.speed_for_up_start);
    SetBoardParamUpQuickSpeed(knit_data, data.board_param.speed_for_up_quick);
    SetBoardParamUpSlowSpeed(knit_data, data.board_param.speed_for_up_slow);
    SetBoardParamToSafeSpeed(knit_data, data.board_param.speed_for_drop_2safe_pos);
    SetBoardParamResetSpeed(knit_data, data.board_param.reset_speed);
    SetBoardParamDownSlowPos(knit_data, data.board_param.pos_for_down_slow);
    SetBoardParamDownSlowSpeed(knit_data, data.board_param.speed_for_down_slow);
    SetBoardParamDropRollSpeed(knit_data, data.board_param.speed_for_roll_when_drop);
    SetBoardParamDropWaitTime(knit_data, data.board_param.wait_for_drop);
    SetBoardParamType(knit_data, 1/*data.board_param.board_type*/);
    SetBoardParamUpWaitPos(knit_data, data.board_param.pos_for_up_wait);
    SetBoardParamRabHookOpenPos(knit_data, data.board_param.hook_open_pos_for_rab);
    SetRabCloseSpeed(knit_data, data.board_param.rab_close_speed);
    SetRabOpenSpeed(knit_data, data.board_param.rab_open_speed);
    SetBoardParamRabClosePos(knit_data, data.board_param.pos_for_rab_close);
    SetBoardParamUpQuickPos(knit_data, data.board_param.pos_for_up_quick);
    SetBoardParamUpSlowPos(knit_data, data.board_param.pos_for_up_slow);
    SetBoardParamDownHookOpenSpeed(knit_data, data.board_param.speed_down_after_hook_open);
    SetBoardParamBalance(knit_data, data.board_param.balance);
    SetBoardParamScissorTimes(knit_data, data.board_param.scissor_times);
    SetBoardParamGauze(knit_data, data.board_param.gauze);
    SetBoardParamKnitMode(knit_data, data.board_param.start_knit_mode);
    SetBoardParamPull(knit_data, data.board_param.pull);
    SetBoardParamCSSyn(knit_data, data.board_param.cs_syn);
    ////////////////////////////////set mac_param data/////////////////////
    SetFuelGab(knit_data,  data.mac_param.fuel_gab);
    SetFuelTime(knit_data, data.mac_param.fuel_time);
    SetRockMode(knit_data, data.mac_param.rock_mode);
    SetRockZeroDir(knit_data, data.mac_param.rock_zero_dir);
    SetRedLightTime(knit_data, data.mac_param.red_light_time);
    SetSeverType(knit_data, data.mac_param.sever_type);
    SetYarnType(knit_data, data.mac_param.yarn_type);
    SetSinkDir(knit_data, data.mac_param.sink_dir);
    SetSelect5ToMacRightLen(knit_data, data.mac_param.select5_to_mac_right_len);
    SetSelect6ToMacRightLen(knit_data, data.mac_param.select6_to_mac_right_len);
    SetMacWidth(knit_data, data.mac_param.mac_width);
    SetSink1ToMacLeftLen(knit_data,data.mac_param.sink1_to_mac_left_len);
    SetSink2ToMacLeftLen(knit_data, data.mac_param.sink2_to_mac_left_len);
    //////////////////////////////set_mac_test_param///////////////////////////////////

    for(i = 0; i < 12; i++)
    {
        for(j = 0; j < 100; j++)
        {
            SetYarnBeforeSet(knit_data, i, j, data.yarn_set[i][j]);
        }
    }

    return MH_FILE_OK;
}
/*
 * write parameter of machine
 */
int WriteMacFile(char file[], struct MH_KNIT *knit_data)
{
    FILE *fp = fopen(file, "w+");
    if(!fp)
    {
        return MH_FILE_ERROR_CREATE;
    }
    MH_MAC_PARAM data;
    memset(&data, 0, sizeof(data));
    MH_FILE_HEADER head;
    memset(&head, 0, sizeof(head));
    head.m = 'M';
    head.h = 'H';
    head.size = sizeof(data);
    if(fwrite(&head, sizeof(head), 1, fp) != 1)
    {
        fclose(fp);
        return MH_FILE_ERROR_WRITE;
    }
    data.type = MH_FILE_TYPE_MAC_PARAM;
    data.size = sizeof(data);
    data.needle_zero = GetNeedleZero(knit_data);
    data.left_yarn_right_com = GetLeftYarnRightCom(knit_data);
    data.left_yarn_left_com = GetLeftYarnLeftCom(knit_data);
    data.right_yarn_right_com = GetRightYarnRightCom(knit_data);
    data.right_yarn_left_com = GetRightYarnLeftCom(knit_data);
    data.left_forbid = GetLeftForbidPos(knit_data);
    data.right_forbid = GetRightForbidPos(knit_data);
    data.inch_type = GetInchType(knit_data);
    data.select_right_com = GetSelectRightCom(knit_data);
    data.select_left_com = GetSelectLeftCom(knit_data);
    data.cam_high_voltage = GetCamHighVoltage(knit_data);
    data.cam_low_voltage = GetCamLowVoltage(knit_data);
    data.select_high_voltage = GetSelectHighVoltage(knit_data);
    data.select_low_voltage = GetSelectLowVoltage(knit_data);
    data.belt_left_com = GetMacLeftCom(knit_data);
    data.belt_right_com = GetMacRightCom(knit_data);
    data.belt_pitch_adjust = GetBeltPitchAdj(knit_data);
    data.dumu_reset_speed = GetDumuResetSpeed(knit_data);
    data.dumu_work_speed = GetDumuWorkSpeed(knit_data);
    data.total_needle = GetTotalNeedle(knit_data);
    data.rock_gap_com = GetRockGapCom(knit_data);
    data.rock_speed = GetRockSpeed(knit_data);
    data.sink_enable = GetSinkEnable(knit_data);
    data.language = GetLanguage(knit_data);
    data.select_repeat_enable = GetSelectRepeatEnable(knit_data);
    data.roll_open_com = GetRollOpenCom(knit_data);
    data.mac_out_enable = GetMacOutEnable(knit_data);
    data.rock_type = GetRockType(knit_data);
    data.rab_percent = GetRabPercent(knit_data);
    data.mac_high_line_speed = GetMacHighLineSpeed(knit_data);
    data.back_sink_reset_value = GetBackSinkResetValue(knit_data);
    data.mac_low_speed = GetMacLowSpeed(knit_data);
    data.mac_reset_speed = GetMacResetSpeed(knit_data);
    data.mac_mid_speed = GetMacMidSpeed(knit_data);
    data.yarn_mode_take = GetYarnTakeMode(knit_data);
    data.auto_line_set = GetAutoLineSet(knit_data);
    data.return_state = GetReturnState(knit_data);
    data.copy_rab_enable = GetRabCopyEnable(knit_data);

    if(data.mac_mid_speed < 50)
    {
        data.mac_mid_speed = 60;
    }
    data.mac_high_speed = GetMacHighSpeed(knit_data);
    if(data.mac_high_speed < 90)
    {
        data.mac_high_speed = 100;
    }
    data.front_sink_reset_value = GetFrontSinkResetValue(knit_data);
    data.rab_stop_torque = GetRabStopTorque(knit_data);
    data.mac_ret_len = GetMacRetLen(knit_data);
    data.start_needle = GetStartNeedle(knit_data);
    data.yarn_before = GetYarnBefore(knit_data);
    data.auto_zero_cloth = GetAutoZeroCloth(knit_data);
    data.save_screen_time = GetSaveScreenTime(knit_data);
    data.save_screen_enable = GetSaveScreenEnable(knit_data);
    data.shake_enable = GetShakeEnable(knit_data);
    data.front_shake_sense = GetFrontShakeSense(knit_data);
    data.back_shake_sense = GetBackShakeSense(knit_data);
    data.mac_num = GetMacNum(knit_data);
    data.front_safe_door_enable = GetFrontSafeDoorEnable(knit_data);
    data.back_safe_door_enable = GetBackSafeDoorEnable(knit_data);
    data.board_enable = GetBoardEnable(knit_data);
    data.scissor_enable = GetScissorEnable(knit_data);
    data.cam_lock = GetCamLock(knit_data);
    data.rock_mid_speed = GetRockMidSpeed(knit_data);
    data.rock_low_speed = GetRockLowSpeed(knit_data);
    data.dumu_work_delta = GetDumuMaxDelta(knit_data);
    data.dumu_reset_delta = GetDumuResetDelta(knit_data);
    data.dumu_reset_warn_enable = GetDumuResetWarnEnable(knit_data);
    data.dumu_before_enable = GetDumuBeforeEnable(knit_data);
    data.dumu_recv_com = GetDumuRecvCom(knit_data);
    data.feeder_start_wait = GetFeederStartWait(knit_data);
    data.feeder_stop_wait = GetFeederStopWait(knit_data);
    data.run_special_enable = GetSpecialRunEnable(knit_data);
    data.sink_work_speed = GetSinkWorkSpeed(knit_data);
    data.rab_run_type = GetRabRunType(knit_data);
    data.roll_stop_torque = GetRollStopTorque(knit_data);
    data.knot_wait_line = GetKnotWaitLine(knit_data);
    data.second_dumu_enable = GetSecondDumuEnable(knit_data);
    data.second_dumu_com = GetSecondDumuCom(knit_data);
    data.hangding_start = GetHangDingStart(knit_data);
    data.hangding_end = GetHangDingEnd(knit_data);
    data.handing_times = GetHangDingTimes(knit_data);
    //0 - 15 needle
    int i;
    for(i = 0; i < 16; ++i)
    {
        data.rock_right_star_com[i] = GetRockRightStarCom(knit_data, i);
        data.rock_right_com[i] = GetRockRightCom(knit_data, i);
        data.rock_left_star_com[i] = GetRockLeftStarCom(knit_data, i);
        data.rock_left_com[i] = GetRockLeftCom(knit_data, i);
        data.rock_right_tran_com[i] = GetRockRightTranCom(knit_data, i);
        data.rock_left_tran_com[i] = GetRockLeftTranCom(knit_data, i);
    }
    data.rab_relax_for_stop = GetStopRabRelax(knit_data);
    int j;
    for(i = 0; i < 2; ++i)
    {
        for(j = 0; j < 8; ++j)
        {
            data.dmbz_single_com[i][j] = GetSingleDmbz(knit_data, i, j);
            data.dmbz_yarn_com[i][j] = GetYarnDmbz(knit_data, i, j);
        }
    }
    //8 sect 16 gauze
    for(i = 0; i < 8; ++i)
    {
        for(j = 0; j < 16; ++j)
        {
            data.yarn_stop[0][i][j] = GetRunYarnStop(knit_data, 0, i, j);
            data.yarn_stop[1][i][j] = GetRunYarnStop(knit_data, 1, i, j);
        }
    }
    //10 sect 8 act
    for(i = 0; i < 10; ++i)
    {
        for(j = 0; j < 8; ++j)
        {
            data.sink_value[i][j] = GetRunSink(knit_data, i, j);
        }
    }
     //10 sect 8 act
    for(i = 0; i < 4; ++i)
    {
        for(j = 0; j < 8; ++j)
        {
            data.cam_value[i][j] = GetCamSettingState(knit_data, i, j);
        }
    }
    //left or right / 20 sect for every 5 speed
    for(i = 0; i < 2; ++i)
    {
        for(j = 0; j < 24; ++j)
        {
            data.yarn_stop_com[i][j] = GetYarnStopCom(knit_data, i, j);
            data.mac_before_com[i][j] = GetMacBeforeCom(knit_data, i, j);
            data.yarn_before_com[i][1][j] = GetYarnBeforeCom(knit_data, i, j);
            data.dumu_before_com[i][j] = GetDumuBeforeCom(knit_data, i, j);
        }
    }
    data.ext_piece_num = GetExtPieceNum(knit_data);
    data.ext_piece_inter = GetExtPieceInter(knit_data);
    data.ext_piece_width = GetExtPieceWidth(knit_data);
    //10 piece(include source) 16 gauze
    for(i = 0; i < 10; ++i)
    {
        for(j = 0; j < 16; ++j)
        {
            data.ext_piece_yarn[i][j] = GetExtPieceYarn(knit_data, i, j);
        }
        data.ext_piece_enable[i] = GetExtPieceEnable(knit_data, i);
    }
    data.left_feeder_enable = GetLeftYarnFeederEnable(knit_data);
    data.right_feeder_enable = GetRightYarnFeederEnable(knit_data);
    //32 sect consistent with mac speed
    for(i = 0; i < 32; ++i)
    {
        data.feeder_speed[i] = GetYarnFeederSpeed(knit_data, i);
    }
    //
    data.probe_shiled = GetProbeShield(knit_data);
    data.knit_state.tran_dumu_enable = GetTranDumuEnable(knit_data);
    data.knit_state.yarn_feeder_mode = GetYarnFeederMode(knit_data);
    data.empty_speed = GetMacEmptySpeed(knit_data);
    data.dumu_stop_add = GetDumuAddPercent(knit_data);
    data.knit_state.yarn_take_mode = GetTakeYarnMode(knit_data);
    data.knit_state.auto_hd_drop = GetAutoHdDropEnable(knit_data);
    data.dumu_max_value = GetDumuMaxValue(knit_data);
    data.knit_state.rab_enable = GetRabEnable(knit_data);
    data.rab_min_param = GetRabMinParam(knit_data);
    data.rab_max_param = GetRabMaxParam(knit_data);
    data.knit_state.roll_enable = GetRollEnable(knit_data);
    data.alarm_mask = GetAlarmMask(knit_data);
    data.dumu_sprt = GetDumuSprt(knit_data);
    //mac parts
    data.mac_part.select1_to_mac_left_len = GetSelect1DistanceToMacLeft(knit_data);
    data.mac_part.select2_to_mac_left_len = GetSelect2DistanceToMacLeft(knit_data);
    data.mac_part.select3_to_mac_left_len = GetSelect3DistanceToMacLeft(knit_data);
    data.mac_part.select4_to_mac_left_len = GetSelect4DistanceToMacLeft(knit_data);
    GetRockDriveData(knit_data, &data.mac_part.rock_drive);
    GetMotorDriveData(knit_data, &data.mac_part.motor_drive);
    data.mac_part.company = GetCompany(knit_data);
    data.mac_part.gauze_high_voltage = GetGauzeHighVoltage(knit_data);
    GetSelect(knit_data, data.mac_part.select);
    GetSysInfo(knit_data, &data.mac_part.sys_info);
    data.mac_part.probe_distance = GetProbeDistance(knit_data);
    data.mac_part.yarn_inter_4cam = GetYarnDistanceOfCam(knit_data);
    data.mac_part.select_sect = GetSelectSect(knit_data);
    data.mac_part.select_start_sect = GetSelectStartSect(knit_data);
    data.mac_part.mac_bit.select_array = GetSelectArray(knit_data);
    data.mac_part.mac_bit.rock_zero_drt = GetRockZeroDrt(knit_data);
    data.mac_part.mac_bit.sink_drt = GetSinkDrt(knit_data);
    data.mac_part.mac_bit.cam_type = GetCamType(knit_data);
    data.mac_part.mac_bit.gauze_type = GetGauzeType(knit_data);
    data.mac_part.mac_bit.driver_type = GetDriverType(knit_data);
    data.mac_part.dumu1_to_mac_left_len = GetDumu1DistanceToMacLeft(knit_data);
    data.mac_part.dumu2_to_mac_left_len = GetDumu2DistanceToMacLeft(knit_data);
    data.mac_part.dumu3_to_mac_left_len = GetDumu3DistanceToMacLeft(knit_data);
    data.mac_part.dumu4_to_mac_left_len = GetDumu4DistanceToMacLeft(knit_data);
    data.mac_param.sink1_to_mac_left_len = GetSink1ToMacLeftLen(knit_data);
    data.mac_param.sink2_to_mac_left_len = GetSink2ToMacLeftLen(knit_data);
    //board
    data.yarn_left_out = GetYarnLeftOut(knit_data);
    data.yarn_right_out = GetYarnRightOut(knit_data);
    data.board_param.high_pos = GetBoardParamHighPos(knit_data);
    data.board_param.safe_pos = GetBoardParamSafePos(knit_data);
    data.board_param.pos_for_roll_close = GetBoardParamRollClosePos(knit_data);
    data.board_param.hook_open_pos_for_roll = GetBoardParamRollHookOpenPos(knit_data);
    data.board_param.delay_for_hook_open = GetBoardParamHookOpenDelay(knit_data);
    data.board_param.speed_for_up_start = GetBoardParamUpStartSpeed(knit_data);
    data.board_param.speed_for_up_quick = GetBoardParamUpQuickSpeed(knit_data);
    data.board_param.speed_for_up_slow = GetBoardParamUpSlowSpeed(knit_data);
    data.board_param.speed_for_drop_2safe_pos = GetBoardParamToSafeSpeed(knit_data);
    data.board_param.reset_speed = GetBoardParamResetSpeed(knit_data);
    data.board_param.pos_for_down_slow = GetBoardParamDownSlowPos(knit_data);
    data.board_param.speed_for_down_slow = GetBoardParamDownSlowSpeed(knit_data);
    data.board_param.speed_for_roll_when_drop = GetBoardParamDropRollSpeed(knit_data);
    data.board_param.wait_for_drop = GetBoardParamDropWaitTime(knit_data);
    data.board_param.board_type = GetBoardParamType(knit_data);
    data.board_param.pos_for_up_wait = GetBoardParamUpWaitPos(knit_data);
    data.board_param.hook_open_pos_for_rab = GetBoardParamRabHookOpenPos(knit_data);
    data.board_param.rab_close_speed = GetRabCloseSpeed(knit_data);
    data.board_param.rab_open_speed = GetRabOpenSpeed(knit_data);
    data.board_param.pos_for_rab_close = GetBoardParamRabClosePos(knit_data);
    data.board_param.pos_for_up_quick = GetBoardParamUpQuickPos(knit_data);
    data.board_param.pos_for_up_slow = GetBoardParamUpSlowPos(knit_data);
    data.board_param.speed_down_after_hook_open = GetBoardParamDownHookOpenSpeed(knit_data);
    data.board_param.balance = GetBoardParamBalance(knit_data);
    data.board_param.scissor_times = GetBoardParamScissorTimes(knit_data);
    data.board_param.gauze = GetBoardParamGauze(knit_data);
    data.board_param.start_knit_mode = GetBoardParamKnitMode(knit_data);
    data.board_param.pull = GetBoardParamPull(knit_data);
    data.board_param.cs_syn = GetBoardParamCSSyn(knit_data);
    ///////////////////////////write mac_param ///////////////////////////
    data.mac_param.fuel_gab = GetFuelGab(knit_data);
    data.mac_param.fuel_time = GetFuelTime(knit_data);
    data.mac_param.rock_mode = GetRockMode(knit_data);
    data.mac_param.rock_zero_dir = GetRockZeroDir(knit_data);
    data.mac_param.red_light_time = GetRedLightTime(knit_data);
    data.mac_param.sever_type = GetSeverType(knit_data);
    data.mac_param.yarn_type = GetYarnType(knit_data);
    data.mac_param.sink_dir = GetSinkDir(knit_data);
    data.mac_param.select5_to_mac_right_len = GetSelect5ToMacRightLen(knit_data);
    data.mac_param.select6_to_mac_right_len = GetSelect6ToMacRightLen(knit_data);
    data.mac_param.mac_width = GetMacWidth(knit_data);

    for(i = 0; i < 12; i++)
    {
        for(j = 0; j < 100; j++)
        {
            data.yarn_set[i][j] = GetYarnBeforeSet(knit_data, i, j);
        }
    }


    if(fwrite(&data, sizeof(data), 1, fp) != 1)
    {
        fclose(fp);
        return MH_FILE_ERROR_DATA;
    }
    fclose(fp);
    return MH_FILE_OK;
}
/*
 * struct of knit param
 */
typedef struct {
    uchar type;
    uchar rsvd;
    ushort version;

    uint size;//==size of MH_FILE_HEADER
    char file_name[256];

    uchar file_type;//Compatible with other productor
    uchar piece_lock;
	uchar gui_speed_switch;
	uchar hangding_enable;

	uchar warn_enable;
    uchar mac_drt;
    uchar sys_state;//run_state
    uchar prefix;

    int aux_index;
    ushort aux_line;
    ushort rsvd2;

    uchar rsvd5[24];

    ushort done_cloth;
    ushort total_cloth;

    ushort curt_line;
    uchar lock_cam;
    uchar bp_enable;

    uint economy_left;
    uint knit_time;
    uint knit_time_count;

    uint done_cloth_backup;
    uint total_cloth_backup;

    uchar fp_enable;
    uchar fp_loop_num;
    ushort stop_line;

    int knit_start;
    int knit_end;
    int sp;

    ushort economy_times;
    ushort economy_list_size;        //循环嵌套

    ushort economy_data[10][3];
    int rsvd4[65];
}MH_KNIT_PARAM;
/*
 * read knit param from file
 */
int ReadKnitFile(char *file, struct MH_KNIT *knit_data)
{
    //int i, j;
    FILE *fp = fopen(file, "r");
    if(!fp)
    {
        return MH_FILE_ERROR_OPEN;
    }
    MH_FILE_HEADER head;
    if(fread(&head, sizeof(head), 1, fp) != 1)
    {
        fclose(fp);
        return MH_FILE_ERROR_READ;
    }
    if(head.m != 'M'|| head.h != 'H')
    {
        fclose(fp);
        return MH_FILE_ERROR_HEADER;
    }
    MH_KNIT_PARAM data;
    if(fread(&data, __min(sizeof(data), head.size), 1, fp) != 1)
    {
        fclose(fp);
        return MH_FILE_ERROR_DATA;
    }
    fclose(fp);
    if(head.size != data.size)
    {
        return MH_FILE_ERROR_FORMAT;
    }
    SetKnitFileName(knit_data, data.file_name);
    SetKnitFileType(knit_data, data.file_type);
    SetPieceLock(knit_data, data.piece_lock);
    SetGuiSpeedType(knit_data, data.gui_speed_switch);
    SetHangDingEnable(knit_data, data.hangding_enable);
    SetWarnEnable(knit_data, data.warn_enable);
    SetTotalCloth(knit_data, data.total_cloth);
    SetDoneCloth(knit_data, data.done_cloth);

    SetCurrentKnitLine(knit_data, data.curt_line);
    SetEconomyLeft(knit_data, data.economy_left);
    SetKnitTime(knit_data, data.knit_time);
    SetKnitCountTime(knit_data, data.knit_time_count);
    SetCamLock(knit_data, data.lock_cam);
    SetBackPieceEnable(knit_data, data.bp_enable);
    SetDoneClothBackup(knit_data, data.done_cloth_backup);
    SetTotalClothBackup(knit_data, data.total_cloth_backup);
    SetFrontPieceEnable(knit_data, data.fp_enable);
    SetStopLine(knit_data, data.stop_line);
    SetCoilStart(knit_data, data.knit_start);
    SetCoilEnd(knit_data, data.knit_end);
    SetCoilKnitStartPoint(knit_data, data.sp);
    SetPrefixKnit(knit_data, data.prefix);
    SetAuxConCurtIndex(knit_data, data.aux_index);
    SetAuxConCurtLine(knit_data, data.aux_line);
    SetWhichNeedDo(knit_data, data.sys_state);
    SetEconomyTimes(knit_data, data.economy_times);
    SetEconomyListSize(knit_data, data.economy_data, data.economy_list_size);

    return MH_FILE_OK;
}
/*
 * write knit param to file
 */
int WriteKnitFile(char *file, struct MH_KNIT *knit_data)
{
    //int i, j;
    FILE *fp = fopen(file, "w+");
    if(!fp)
    {
        return MH_FILE_ERROR_CREATE;
    }
    MH_KNIT_PARAM data;
    memset(&data, 0, sizeof(data));
    MH_FILE_HEADER head;
    memset(&head, 0, sizeof(head));
    head.m = 'M';
    head.h = 'H';
    head.size = sizeof(data);
    if(fwrite(&head, sizeof(head), 1, fp) != 1)
    {
        fclose(fp);
        return MH_FILE_ERROR_WRITE;
    }
    data.type = MH_FILE_TYPE_KNIT_PARAM;
    data.size = sizeof(data);
    char *sc = GetKnitFileName(knit_data);
    data.file_name[0] = 0;
    if(sc)
    {
        if(strlen(sc) < sizeof(data.file_name))
        {
            strcat(data.file_name, sc);
        }
    }
    data.file_type = GetKnitFileType(knit_data);
    data.piece_lock = GetPieceLock(knit_data);
    data.gui_speed_switch = GetGuiSpeedType(knit_data);
    data.hangding_enable = GetHangDingEnable(knit_data);
    data.warn_enable = GetWarnEnable(knit_data);
    data.done_cloth = GetDoneCloth(knit_data);
    data.total_cloth = GetTotalCloth(knit_data);
    data.curt_line = GetCurrentKnitLine(knit_data);
    data.economy_left = GetEconomyLeft(knit_data);
    data.knit_time = GetKnitTime(knit_data);
    data.knit_time_count = GetKnitCountTime(knit_data);
    data.lock_cam = GetCamLock(knit_data);
    data.bp_enable = GetBackPieceEnable(knit_data);
    data.done_cloth_backup = GetDoneClothBackup(knit_data);
    data.total_cloth_backup = GetTotalClothBackup(knit_data);

    data.fp_enable = GetFrontPieceEnable(knit_data);
    data.stop_line = GetStopLine(knit_data);
    data.knit_start = GetCoilStart(knit_data);
    data.knit_end = GetCoilEnd(knit_data);
    data.sp = GetCoilKnitStartPoint(knit_data);
    data.prefix = IsPrefixKnit(knit_data);
    data.aux_index = GetAuxConCurtIndex(knit_data);
    data.aux_line = GetAuxConCurtLine(knit_data);
    data.economy_times = GetEconomyTimes(knit_data);
    data.economy_list_size = GetEconomyListSize(knit_data, data.economy_data, 10);

    /*which to do after power on: reset, wait, goon knit*/
    data.sys_state = GetWhichNeedDo(knit_data);

    if(fwrite(&data, sizeof(data), 1, fp) != 1)
    {
        fclose(fp);
        return MH_FILE_ERROR_DATA;
    }
    fclose(fp);
    return MH_FILE_OK;
}
#pragma pack(1)
struct _CARRIAGE_FILE {
    uchar type;
    uchar rsvd;
    ushort version;
    uint size;
    CARRIAGE_STATE carriage;
};
#pragma pack()
typedef struct _CARRIAGE_FILE  CARRIAGE_FILE;
/*
 * read carriage state for knit continue
 */
int ReadKnitContinue(char *file, PCARRIAGE_STATE pcarriage)
{
    FILE *fp = fopen(file, "r");
    if(!fp)
    {
        return MH_FILE_ERROR_OPEN;
    }
    MH_FILE_HEADER head;
    if(fread(&head, sizeof(head), 1, fp) != 1)
    {
        fclose(fp);
        return MH_FILE_ERROR_READ;
    }
    if(head.m != 'M'|| head.h != 'H')
    {
        fclose(fp);
        return MH_FILE_ERROR_HEADER;
    }
    CARRIAGE_FILE data;
    if(fread(&data, __min(sizeof(data), head.size), 1, fp) != 1)
    {
        fclose(fp);
        return MH_FILE_ERROR_DATA;
    }
    fclose(fp);
    if(head.size != data.size)
    {
        return MH_FILE_ERROR_FORMAT;
    }
    memcpy(pcarriage, &data.carriage, sizeof(data.carriage));
    return MH_FILE_OK;
}
/*
 * write carriage state for knit continue
 */
int WriteKnitContinue(char *file, PCARRIAGE_STATE pcarriage)
{
    FILE *fp = fopen(file, "w+");
    if(!fp)
    {
        return MH_FILE_ERROR_CREATE;
    }
    MH_FILE_HEADER head;
    memset(&head, 0, sizeof(head));
    CARRIAGE_FILE data;
    memset(&data, 0, sizeof(data));
    head.m = 'M';
    head.h = 'H';
    head.size = sizeof(data);
    if(fwrite(&head, sizeof(head), 1, fp) != 1)
    {
        fclose(fp);
        return MH_FILE_ERROR_WRITE;
    }
    data.type = MH_FILE_TYPE_CARRIAGE_STATE;
    data.size = sizeof(data);
    memcpy(&data.carriage, pcarriage, sizeof(data.carriage));
    if(fwrite(&data, sizeof(data), 1, fp) != 1)
    {
        fclose(fp);
        return MH_FILE_ERROR_DATA;
    }
    fclose(fp);
    return MH_FILE_OK;
}
/*
 *
 */
struct _CONF_PARAM {
    uchar type;
    uchar rsvd;
    ushort version;
    uint size;
    uchar exchange_yarn[2][16];
};
typedef struct _CONF_PARAM CONF_PARAM;
/*
 * read configure file for every task
 */
int ReadConfigureFile(char *file, struct MH_KNIT *knit_data)
{
    int i;
    FILE *fp = fopen(file, "r");
    if(!fp)
    {
        return MH_FILE_ERROR_OPEN;
    }
    MH_FILE_HEADER head;
    if(fread(&head, sizeof(head), 1, fp) != 1)
    {
        fclose(fp);
        return MH_FILE_ERROR_READ;
    }
    if(head.m != 'M' || head.h != 'H')
    {
        fclose(fp);
        return MH_FILE_ERROR_HEADER;
    }
    CONF_PARAM data;
    if(fread(&data, __min(sizeof(data), head.size), 1, fp) != 1)
    {
        fclose(fp);
        return MH_FILE_ERROR_DATA;
    }
    fclose(fp);
    if(head.size != data.size)
    {
        return MH_FILE_ERROR_FORMAT;
    }
    for(i = 0; i < 16; ++i)
    {
        SetExchangedYarn(knit_data, 0, i, data.exchange_yarn[0][i]);
        SetExchangedYarn(knit_data, 1, i, data.exchange_yarn[1][i]);
    }
    return MH_FILE_OK;
}
/*
 * write configure file for every task
 */
int WriteConfigureFile(char *file, struct MH_KNIT *knit_data)
{
    FILE *fp = fopen(file, "w+");
    if(!fp)
    {
        return MH_FILE_ERROR_CREATE;
    }
    MH_FILE_HEADER head;
    memset(&head, 0, sizeof(head));
    CONF_PARAM data;
    memset(&data, 0, sizeof(data));
    head.m = 'M';
    head.h = 'H';
    head.size = sizeof(data);
    if(fwrite(&head, sizeof(head), 1, fp) != 1)
    {
        fclose(fp);
        return MH_FILE_ERROR_WRITE;
    }
    data.type = MH_FILE_TYPE_CONF_PARAM;
    data.size = sizeof(data);
    int i = 0;
    for(i = 0; i < 16; ++i)
    {
        data.exchange_yarn[0][i] = GetExchangedYarn(knit_data, 0, i);
        data.exchange_yarn[1][i] = GetExchangedYarn(knit_data, 1, i);
    }
    if(fwrite(&data, sizeof(data), 1, fp) != 1)
    {
        fclose(fp);
        return MH_FILE_ERROR_DATA;
    }
    fclose(fp);
    return MH_FILE_OK;
}
/*
 * read sys log file
 */
int ReadSysLogFile(char *file, SYS_LOG *list_head)
{
    int i = 0;
    FILE *fp = fopen(file, "r");
    if(!fp)
    {
        return MH_FILE_ERROR_OPEN;
    }
    MH_FILE_HEADER head;
    if(fread(&head, sizeof(head), 1, fp) != 1)
    {
        fclose(fp);
        return MH_FILE_ERROR_READ;
    }
    if(head.m != 'M' || head.h != 'H')
    {
        fclose(fp);
        return MH_FILE_ERROR_HEADER;
    }

    SYS_LOG log_head;
    if(fread(&log_head, sizeof(log_head), 1, fp) != 1)
    {
        fclose(fp);
        return MH_FILE_ERROR_DATA;
    }
    if(head.size != log_head.total)
    {
        fclose(fp);
        return MH_FILE_ERROR_FORMAT;
    }
    memcpy(list_head, &log_head, sizeof(SYS_LOG));

    if(log_head.total)
    {
        SYS_LOG *data = calloc(log_head.total, sizeof(SYS_LOG));
        if(fread(data, sizeof(SYS_LOG), log_head.total, fp) != log_head.total)
        {
            free(data);
            list_head->next = NULL;
            list_head->total = 0;
            fclose(fp);
            return MH_FILE_ERROR_DATA;
        }
        list_head->next = NULL;
        for(i = log_head.total - 1; i >= 0; --i)
        {
            SYS_LOG *q = calloc(1, sizeof(SYS_LOG));
            memcpy(q, &data[i], sizeof(SYS_LOG));
            q->next = list_head->next;
            list_head->next = q;
        }
        free(data);
    }
    fclose(fp);
    return MH_FILE_OK;
}
/*
 * write sys log file
 */
int WriteSysLogFile(char *file, SYS_LOG *list_head)
{

    FILE *fp = fopen(file, "w+");
    if(!fp)
    {
        return MH_FILE_ERROR_CREATE;
    }
    MH_FILE_HEADER head;
    memset(&head, 0, sizeof(head));
    head.m = 'M';
    head.h = 'H';
    head.size = list_head->total;
    if(fwrite(&head, sizeof(head), 1, fp) != 1)
    {
        fclose(fp);
        return MH_FILE_ERROR_WRITE;
    }
    if(fwrite(list_head, sizeof(SYS_LOG), 1, fp) != 1)
    {
        fclose(fp);
        return MH_FILE_ERROR_FORMAT;
    }
    if(list_head->total)
    {
        SYS_LOG *log = calloc(list_head->total, sizeof(SYS_LOG));
        SYS_LOG *next = list_head->next;
        int i = 0;
        while(next)
        {
            memcpy(&log[i], next, sizeof(SYS_LOG));
            log[i].next = NULL;
            ++i;
            next = next->next;
        }
        if(fwrite(log, sizeof(SYS_LOG), list_head->total, fp) != list_head->total)
        {
            free(log);
            fclose(fp);
            return MH_FILE_ERROR_DATA;
        }
        free(log);
    }
    fclose(fp);
    return MH_FILE_OK;
}
/*
 * read SN file
 */
int ReadSNFile(char *file, char *sc, int size)
{
    FILE *fp = fopen(file, "r");
    if(!fp)
    {
        return MH_FILE_ERROR_OPEN;
    }
    if(fread(sc, size, 1, fp) != 1)
    {
        fclose(fp);
        return MH_FILE_ERROR_READ;
    }
    fclose(fp);
    return MH_FILE_OK;
}
/*
 * write sn file
 */
int WriteSNFile(char *file, const char *sc, int size)
{
    FILE *fp = fopen(file, "w+");
    if(!fp)
    {
        return MH_FILE_ERROR_CREATE;
    }
    if(fwrite(sc, size, 1, fp) != 1)
    {
        fclose(fp);
        return MH_FILE_ERROR_DATA;
    }
    fclose(fp);
    return MH_FILE_OK;
}
/*
 * read raw file
 */
int ReadRawFile(const char *path, void *buf, int size)
{
    //int file = open (path, O_RDONLY);
    FILE *fp = fopen(path, "r+");
    if (!fp)
    {
        return -1;
    }
    memset(buf, 0, size);
    printf("read size = %d, path = %s\n",size, path);
    if(fread(buf, size, 1, fp) != 1)
    {
        fclose(fp);
        return MH_FILE_ERROR_READ;
    }
    //read(file, buf, size);
    fclose(fp);
    return 0;
}
/*
 *
 */
int WriteRawFile(const char *file, void *data, int size)
{
    FILE *fp = fopen(file, "w+");
    if(!fp)
    {
        return MH_FILE_ERROR_CREATE;
    }
   printf("write size = %d, path = %s\n",size, file);
    if(fwrite(data, size, 1, fp) != 1)
    {
        fclose(fp);
        return MH_FILE_ERROR_WRITE;
    }
    fclose(fp);
    return MH_FILE_OK;
}
/*
 * read raw data of dumu
 * param num: number of dumu item
 */
int ReadRawDumuFile(char *file, ushort *dumu, int size)
{
    FILE *fp = fopen(file, "r");
    if(!fp)
    {
        return MH_FILE_ERROR_OPEN;
    }
    if(fread(dumu, size, 1, fp) != 1)
    {
        fclose(fp);
        return MH_FILE_ERROR_READ;
    }
    fclose(fp);
    return MH_FILE_OK;
}
/*
 *
 */
int WriteRawDumuFile(char *file, ushort *dumu, int size)
{
    FILE *fp = fopen(file, "w+");
    if(!fp)
    {
        return MH_FILE_ERROR_CREATE;
    }
    if(fwrite(dumu, size, 1, fp) != 1)
    {
        fclose(fp);
        return MH_FILE_ERROR_WRITE;
    }
    fclose(fp);
    return MH_FILE_OK;
}
/*
 * read dumu file to knit data and send data to machine
 */
int ReadDumuFile(char *file, struct MH_KNIT *knit_data)
{
    FILE *fp = fopen(file, "r");
    if(!fp)
    {
        return MH_FILE_ERROR_OPEN;
    }
    ushort dumu[9][32];
    if(fread(dumu, sizeof(dumu), 1, fp) != 1)
    {
        fclose(fp);
        return MH_FILE_ERROR_READ;
    }
    fclose(fp);
    int i = 0;
    for(i = 0; i < 32; ++i)
    {
        SetRunDumu(knit_data, 0, i, 0, dumu[7][i]);
        SetRunDumu(knit_data, 0, i, 1, dumu[0][i]);
        SetRunDumu(knit_data, 0, i, 2, dumu[5][i]);
        SetRunDumu(knit_data, 0, i, 3, dumu[2][i]);
        SetRunDumu(knit_data, 1, i, 0, dumu[8][i]);
        SetRunDumu(knit_data, 1, i, 1, dumu[1][i]);
        SetRunDumu(knit_data, 1, i, 2, dumu[6][i]);
        SetRunDumu(knit_data, 1, i, 3, dumu[3][i]);
    }
    return MH_FILE_OK;
}
/*
 * read dumu file to knit data only
 */
int ReadDumuData(char *file, struct MH_KNIT *knit_data)
{
    FILE *fp = fopen(file, "r");
    if(!fp)
    {
        return MH_FILE_ERROR_OPEN;
    }
    ushort dumu[9][32];
    if(fread(dumu, sizeof(dumu), 1, fp) != 1)
    {
        fclose(fp);
        return MH_FILE_ERROR_READ;
    }
    fclose(fp);
    int i = 0;
    for(i = 0; i < 32; ++i)
    {
        SetRunDumuData(knit_data, 0, i, 0, dumu[7][i]);
        SetRunDumuData(knit_data, 0, i, 1, dumu[0][i]);
        SetRunDumuData(knit_data, 0, i, 2, dumu[5][i]);
        SetRunDumuData(knit_data, 0, i, 3, dumu[2][i]);
        SetRunDumuData(knit_data, 1, i, 0, dumu[8][i]);
        SetRunDumuData(knit_data, 1, i, 1, dumu[1][i]);
        SetRunDumuData(knit_data, 1, i, 2, dumu[6][i]);
        SetRunDumuData(knit_data, 1, i, 3, dumu[3][i]);
    }
    return MH_FILE_OK;
}
/*
 * write dumu file
 */
int WriteDumuFile(char *file, struct MH_KNIT *knit_data)
{
    FILE *fp = fopen(file, "w+");
    if(!fp)
    {
        return MH_FILE_ERROR_CREATE;
    }
    ushort dumu[9][32];
    int i = 0;
    for(i = 0; i < 32; ++i)
    {
        dumu[7][i] = GetRunDumu(knit_data, 0, i, 0);//1
        dumu[0][i] = GetRunDumu(knit_data, 0, i, 1);//2
        dumu[5][i] = GetRunDumu(knit_data, 0, i, 2);//3
        dumu[2][i] = GetRunDumu(knit_data, 0, i, 3);//4
        dumu[8][i] = GetRunDumu(knit_data, 1, i, 0);//5
        dumu[1][i] = GetRunDumu(knit_data, 1, i, 1);//6
        dumu[6][i] = GetRunDumu(knit_data, 1, i, 2);//7
        dumu[3][i] = GetRunDumu(knit_data, 1, i, 3);//8
    }
    if(fwrite(dumu, sizeof(dumu), 1, fp) != 1)
    {
        fclose(fp);
        return MH_FILE_ERROR_WRITE;
    }
    fclose(fp);
    return MH_FILE_OK;
}
/*
 * read raw data of speed from file
 */
int ReadRawSpeedFile(char *file, uchar *speed, int size)
{
    FILE *fp = fopen(file, "r");
    if(!fp)
    {
        return MH_FILE_ERROR_OPEN;
    }
    if(fread(speed, size, 1, fp) != 1)
    {
        fclose(fp);
        return MH_FILE_ERROR_READ;
    }
    fclose(fp);
    return MH_FILE_OK;
}
/*
 * write raw data of speed to file
 */
int WriteRawSpeedFile(char *file, uchar *speed, int size)
{
    FILE *fp = fopen(file, "w+");
    if(!fp)
    {
        return MH_FILE_ERROR_CREATE;
    }
    if(fwrite(speed, size, 1, fp) != 1)
    {
        fclose(fp);
        return MH_FILE_ERROR_WRITE;
    }
    fclose(fp);
    return MH_FILE_OK;
}
/*
 * read speed file into knit data
 */
int ReadSpeedData(char *file, struct MH_KNIT *knit_data)
{
    FILE *fp = fopen(file, "r");
    if(!fp)
    {
        return MH_FILE_ERROR_OPEN;
    }
    uchar speed[6][32];
    if(fread(speed, sizeof(speed), 1, fp) != 1)
    {
        fclose(fp);
        return MH_FILE_ERROR_READ;
    }
    fclose(fp);
    int i = 0;
    for(i = 0; i < 32; ++i)
    {
        SetMacRunSpeedData(knit_data, i, speed[1][i]);
        SetRabRunSpeed(knit_data, i, speed[4][i]);
        SetRollRunSpeed(knit_data, i, speed[3][i]);
        SetSwitchRunSpeed(knit_data, i, speed[5][i]);
    }
    return MH_FILE_OK;
}
/*
 * read speed file into knit data
 */
int ReadSpeedFile(char *file, struct MH_KNIT *knit_data)
{
    FILE *fp = fopen(file, "r");
    if(!fp)
    {
        return MH_FILE_ERROR_OPEN;
    }
    uchar speed[6][32];
    if(fread(speed, sizeof(speed), 1, fp) != 1)
    {
        fclose(fp);
        return MH_FILE_ERROR_READ;
    }
    fclose(fp);
    int i = 0;
    for(i = 0; i < 32; ++i)
    {
        if(speed[0][i] & 0x80)
        {
            SetRudeDumu(knit_data, i, speed[0][i] | 0xFF00);
        }
        else
        {
            SetRudeDumu(knit_data, i, speed[0][i]);
        }
        SetMacRunSpeed(knit_data, i, speed[1][i]);
        SetRabRunSpeed(knit_data, i, speed[4][i]);
        SetRollRunSpeed(knit_data, i, speed[3][i]);
        SetSwitchRunSpeed(knit_data, i, speed[5][i]);
    }
    return MH_FILE_OK;
}
/*
 * write speed file
 */
int WriteSpeedFile(char *file, struct MH_KNIT *knit_data)
{
    FILE *fp = fopen(file, "w+");
    if(!fp)
    {
        return MH_FILE_ERROR_CREATE;
    }
    uchar speed[6][32];
    int i = 0;
    for(i = 0; i < 32; ++i)
    {
        speed[0][i] = GetRudeDumu(knit_data, i);
        speed[1][i] = GetMacRunSpeed(knit_data, i);
        speed[4][i] = GetRabRunSpeed(knit_data, i);
        speed[3][i] = GetRollRunSpeed(knit_data, i);
        speed[5][i] = GetSwitchRunSpeed(knit_data, i);
    }
    if(fwrite(speed, sizeof(speed), 1, fp) != 1)
    {
        fclose(fp);
        return MH_FILE_ERROR_WRITE;
    }
    fclose(fp);
    return MH_FILE_OK;
}
/*
 * read speed file into knit data
 */
int ReadStopFile(char *file, struct MH_KNIT *knit_data)
{
    FILE *fp = fopen(file, "r");
    if(!fp)
    {
        return MH_FILE_ERROR_OPEN;
    }
    struct
    {
        ushort stop1[8][2][8]; //1-8
        ushort stop9[8][2][8]; //9-16
        ushort gout[16][2];
    } stop;
    if(fread(&stop, sizeof(stop), 1, fp) != 1)
    {
        fclose(fp);
        return MH_FILE_ERROR_READ;
    }
    fclose(fp);
    int i = 0, j = 0;
    for(i = 0; i < 8; ++i)
    {
        for(j = 0; j < 8; ++j)
        {
            SetRunYarnStop(knit_data, 0, i, j, stop.stop1[i][0][j]);
            SetRunYarnStop(knit_data, 1, i, j, stop.stop1[i][1][j]);
            SetRunYarnStop(knit_data, 0, i, j + 8, stop.stop9[i][0][j]);
            SetRunYarnStop(knit_data, 1, i, j + 8, stop.stop9[i][1][j]);
        }
    }
    for(i = 0; i < 16; ++i)
    {
        SetRunYarnOut(knit_data, 0, i, stop.gout[i][0]);
        SetRunYarnOut(knit_data, 1, i, stop.gout[i][1]);
    }
    return MH_FILE_OK;
}
/*
 *
 */
int WriteStopFile(char *file, struct MH_KNIT *knit_data)
{
    FILE *fp = fopen(file, "w+");
    if(!fp)
    {
        return MH_FILE_ERROR_CREATE;
    }
    struct {
        ushort stop1[8][2][8]; //1-8
        ushort stop9[8][2][8]; //9-16
        ushort gout[16][2];
    }stop;
    int i = 0, j = 0;
    for(i = 0; i < 8; ++i)
    {
        for(j = 0; j < 8; ++j)
        {
            stop.stop1[i][0][j] = GetRunYarnStop(knit_data, 0, i, j);
            stop.stop1[i][1][j] = GetRunYarnStop(knit_data, 1, i, j);
            stop.stop9[i][0][j] = GetRunYarnStop(knit_data, 0, i, j + 8);
            stop.stop9[i][1][j] = GetRunYarnStop(knit_data, 1, i, j + 8);
        }
    }
    for(i = 0; i < 16; ++i)
    {
        stop.gout[i][0] = GetRunYarnOut(knit_data, 0, i);
        stop.gout[i][1] = GetRunYarnOut(knit_data, 1, i);
    }
    if(fwrite(&stop, sizeof(stop), 1, fp) != 1)
    {
        fclose(fp);
        return MH_FILE_ERROR_WRITE;
    }
    fclose(fp);
    return MH_FILE_OK;
}
/*
 * read raw data of flower
 * sizeof(head) == 16
 * after this, do not forget to free (*flw)
 */
int ReadRawPatFile(char *file, ushort *head, uchar **flw)
{
    FILE *fp = fopen(file, "r");
    if(!fp)
    {
        return MH_FILE_ERROR_OPEN;
    }
    if(fread(head, 16, 1, fp) != 1)
    {
        fclose(fp);
        return MH_FILE_ERROR_READ;
    }
    if(!head[0] || !head[1])
    {
        fclose(fp);
        return MH_FILE_ERROR_FORMAT;
    }
    uchar *data = (uchar*)calloc(1, (head[0] + 1) / 2 * head[1]);
    if(!data)
    {
        fclose(fp);
        return MH_FILE_ERROR_MEM;
    }
    if(fread(data, (head[0] + 1) / 2 * head[1], 1, fp) != 1)
    {
        fclose(fp);
        free(data);
        return MH_FILE_ERROR_DATA;
    }
    fclose(fp);
    *flw = data;
    return MH_FILE_OK;
}
#if 0
/*
 * write raw data to file
 */
int WriteRawDatFile(char *file, ushort *head, uchar *flw)
{
    FILE *fp = fopen(file, "w+");
    if(!fp)
    {
        return MH_FILE_ERROR_CREATE;
    }
    if(fwrite(head, 16, 1, fp) != 1)
    {
        fclose(fp);
        return MH_FILE_ERROR_WRITE;
    }
    if(fwrite(flw, (head[0] + 1) / 2 * head[1], 1, fp) != 1)
    {
        fclose(fp);
        return MH_FILE_ERROR_DATA;
    }
    fclose(fp);
    return MH_FILE_OK;
}
#endif
/*
 * read dat file
 */
int ReadDatFile(char *file, struct MH_KNIT *knit_data)
{
    FILE *fp = fopen(file, "r");
    if(!fp)
    {
        return MH_FILE_ERROR_OPEN;
    }
    ushort head[8] = {0};
    if(fread(head, sizeof(head), 1, fp) != 1)
    {
        fclose(fp);
        return MH_FILE_ERROR_READ;
    }
    if(!head[0] || !head[1])
    {
        fclose(fp);
        return MH_FILE_ERROR_FORMAT;
    }
    uchar *data = (uchar*)calloc(1, (head[0] + 1) / 2 * head[1]);
    if(!data)
    {
        fclose(fp);
        return MH_FILE_ERROR_MEM;
    }
    if(fread(data, (head[0] + 1) / 2 * head[1], 1, fp) != 1)
    {
        fclose(fp);
        free(data);
        return MH_FILE_ERROR_DATA;
    }
    SP_HEAD aux_head;
    if(fread(&aux_head, sizeof(aux_head), 1, fp) == 1)
    {
        int ret = AuxConCheck(&aux_head);
        if(ret)
        {
            printf("aux con check failed: %d\n", ret);
        }
        else
        {
            KNIT_PAGE *pcon_buf = calloc(1, aux_head.total_rows * sizeof(KNIT_PAGE));
            if(fread(pcon_buf, aux_head.total_rows * sizeof(KNIT_PAGE), 1, fp) == 1)
            {
                ret = CreateAuxConList(knit_data, pcon_buf, aux_head.total_rows);
                printf("aux con line : %d for %d\n", aux_head.total_rows, ret);
            }
            else
            {
                printf("read %d aux con failed\n", aux_head.total_rows);
            }
            free(pcon_buf);
        }
    }
    fclose(fp);
    uchar *p = CreateFlowerData(knit_data, head[0], head[1]);
    if(p)
    {
        int w, h;
        for(h = 0; h < head[1]; h++)
        {
            for(w = 0; w < head[0]; ++w)
            {
                uchar c = data[(head[0] + 1) / 2 * h + w / 2];
                p[head[0] * h + w] = ((w & 1) ? (c) : (c >> 4)) & 0x0F;
            }
        }
    }
    free(data);
    return MH_FILE_OK;
}
/*
 * write dat file
 */
int WriteDatFile(char *file, struct MH_KNIT *knit_data)
{
    uchar *p = GetFlowerData(knit_data);
    if(!p)
    {
        return MH_FILE_OK;
    }
    int nWidth = GetFlowerWidth(knit_data);
    int nHeight = GetFlowerHeight(knit_data);
    uchar *data = (uchar*)calloc(1, (nWidth + 1) / 2 * nHeight);
    if(!data)
    {
        return MH_FILE_ERROR_MEM;
    }
    int w, h;
    for(h = 0; h < nHeight; ++h)
    {
        for(w = 0; w < nWidth; ++w)
        {
            uchar c = p[nWidth * h + w];
            data[(nWidth + 1) / 2 * h + w / 2] |= (w & 1) ? (c & 0x0F) : (c << 4);
        }
    }
    FILE *fp = fopen(file, "w+");
    if(!fp)
    {
        free(data);
        return MH_FILE_ERROR_CREATE;
    }
    ushort head[8] = {0};
    head[0] = (nWidth + 1) / 2 * 2;
    head[1] = nHeight;
    if(fwrite(head, sizeof(head), 1, fp) != 1)
    {
        free(data);
        fclose(fp);
        return MH_FILE_ERROR_WRITE;
    }
    if(fwrite(data, (nWidth + 1) / 2 * nHeight, 1, fp) != 1)
    {
        free(data);
        fclose(fp);
        return MH_FILE_ERROR_DATA;
    }
    free(data);
    SP_HEAD aux_head;
    aux_head.total_rows = GetAuxConTotalRows(knit_data);
    if(aux_head.total_rows)
    {
        aux_head.signature = 0x66AA;
        KNIT_PAGE *paux = calloc(aux_head.total_rows, sizeof(KNIT_PAGE));
        GetAuxConData(knit_data, paux, aux_head.total_rows);
        if(fwrite(&aux_head, sizeof(aux_head), 1, fp) == 1)
        {
            if(fwrite(paux, aux_head.total_rows * sizeof(KNIT_PAGE), 1, fp) != 1)
            {
                printf("write aux con failed\n");
            }
        }
        else
        {
            printf("write aux con head failed\n");
        }
    }
    fclose(fp);
    return MH_FILE_OK;
}
/*
 * read raw control data from file
 * after this, do not forget to free (*con)
 */
int ReadRawConFile(char *file, ushort *height, KNIT_PAGE **con, KNIT_PAGE *head)
{
    FILE *fp = fopen(file, "r");
    if(!fp)
    {
        return MH_FILE_ERROR_OPEN;
    }
    long size = 0;
    if(!fseek(fp, 0L, SEEK_END))
    {
        size = ftell(fp);
    }
    printf("read con size = %d, path = %s\n",(int)size, file);
    if(fseek(fp, 0, SEEK_SET))
    {
        fclose(fp);
        return MH_FILE_ERROR_SEEK;
    }
    if(size < sizeof(KNIT_PAGE) || (size % sizeof(KNIT_PAGE)))
    {
        fclose(fp);
        return MH_FILE_ERROR_FORMAT;
    }
    ushort nHeight = size / sizeof(KNIT_PAGE) - 1;
    if(!nHeight)
    {
        fclose(fp);
        return MH_FILE_ERROR_DATA;
    }
    else if(head)
    {
        if(fread(head, sizeof(KNIT_PAGE), 1, fp) != 1)
        {
            fclose(fp);
            return MH_FILE_ERROR_READ;
        }
    }
    else
    {
        if(fseek(fp, sizeof(KNIT_PAGE), SEEK_SET))
        {
            fclose(fp);
            return MH_FILE_ERROR_SEEK;
        }
    }
    KNIT_PAGE *p = (KNIT_PAGE*)calloc(sizeof(KNIT_PAGE), nHeight);
    if(!p)
    {
        fclose(fp);
        return MH_FILE_ERROR_MEM;
    }
    if(fread(p, sizeof(KNIT_PAGE), nHeight, fp) != nHeight)
    {
        fclose(fp);
        free(p);
        return MH_FILE_ERROR_READ;
    }
    *height = nHeight;
    fclose(fp);
    *con = p;
    return MH_FILE_OK;
}
/*
 *
 */
int WriteRawConFile(char *file, ushort height, KNIT_PAGE *con, KNIT_PAGE *head)
{
    FILE *fp = fopen(file, "w+");
    if(!fp)
    {
        return MH_FILE_ERROR_CREATE;
    }
    if(head)
    {
        if(fwrite(head, sizeof(KNIT_PAGE), 1, fp) != 1)
        {
            fclose(fp);
            return MH_FILE_ERROR_WRITE;
        }
    }
    else if(fseek(fp, sizeof(KNIT_PAGE), SEEK_SET))
    {
        fclose(fp);
        return MH_FILE_ERROR_SEEK;
    }
    if(fwrite(con, sizeof(KNIT_PAGE), height, fp) != height)
    {
        fclose(fp);
        return MH_FILE_ERROR_WRITE;
    }
    fclose(fp);
    return MH_FILE_OK;
}
/*
 * read control file
 */
int ReadConFile(char *file, struct MH_KNIT *knit_data)
{
    FILE *fp = fopen(file, "r");
    if(!fp)
    {
        return MH_FILE_ERROR_OPEN;
    }
    long size = 0;
    if(!fseek(fp, 0L, SEEK_END))
    {
        size = ftell(fp);
    }
    if(size < sizeof(KNIT_PAGE) || (size % sizeof(KNIT_PAGE)))
    {
        fclose(fp);
        return MH_FILE_ERROR_FORMAT;
    }
    ushort nHeight = size / sizeof(KNIT_PAGE) - 1;
    if(!nHeight)
    {
        fclose(fp);
        return MH_FILE_ERROR_DATA;
    }
    else
    {
        if(fseek(fp, sizeof(KNIT_PAGE), SEEK_SET))
        {
            fclose(fp);
            return MH_FILE_ERROR_SEEK;
        }
    }
    KNIT_PAGE *p = (KNIT_PAGE*)CreateKnitPage(knit_data, nHeight);
    if(!p)
    {
        fclose(fp);
        return MH_FILE_ERROR_MEM;
    }
    if(fread(p, sizeof(KNIT_PAGE), nHeight, fp) != nHeight)
    {
        fclose(fp);
        free(p);
        SetTotalKnitPage(knit_data, 0);
        SetControlData(knit_data, 0);
        return MH_FILE_ERROR_READ;
    }
    fclose(fp);
    return MH_FILE_OK;
}
/*
 * write con file
 */
int WriteConFile(char *file, struct MH_KNIT *knit_data)
{
    KNIT_PAGE *p = (KNIT_PAGE*)GetControlData(knit_data);
    if(!p)
    {
        return MH_FILE_OK;
    }
    FILE *fp = fopen(file, "w+");
    if(!fp)
    {
        return MH_FILE_ERROR_CREATE;
    }
    if(fseek(fp, sizeof(KNIT_PAGE), SEEK_SET))
    {
        fclose(fp);
        return MH_FILE_ERROR_SEEK;
    }
    ushort nHeight = GetTotalKnitPage(knit_data);
    if(fwrite(p, sizeof(KNIT_PAGE), nHeight, fp) != nHeight)
    {
        fclose(fp);
        return MH_FILE_ERROR_WRITE;
    }
    fclose(fp);
    return MH_FILE_OK;
}
/*
 * get suffix name for machine produced by every company
 */
int GetSuffixName(char *des, char *src)
{
    des[0] = 0;
    int ret = 0;
    if(!strcmp(src, ".con"))
    {
        strcat(des, ".dat");
    }
    else if(!strcmp(src, ".dat"))
    {
        strcat(des, ".con");
    }
    else if(!strcmp(src, ".CON"))
    {
        strcat(des, ".DAT");
    }
    else if(!strcmp(src, ".DAT"))
    {
        strcat(des, ".CON");
    }
    else if(!strcmp(src, ".PAT"))
    {
        strcat(des, ".CNT");
    }
    else if(!strcmp(src, ".CNT"))
    {
        strcat(des, ".PAT");
    }
    else
    {
        ret = -1;
    }
    return ret;
}
/*
 * copy dumu between file
 */
int CopyDumuFile(char *from, char *to)
{
    ushort dumu[9][32];
    int ret = ReadRawDumuFile(from, (ushort*)dumu, sizeof(dumu));
    if(ret)
    {
        ret = MH_FILE_ERROR_READ;
    }
    else
    {
        ret = WriteRawDumuFile(to, (ushort*)dumu, sizeof(dumu));
        if(ret)
        {
            ret = MH_FILE_ERROR_WRITE;
        }
    }
    return ret;
}
/*
 * copy speed between file
 */
int CopySpeedFile(char *from, char *to)
{
    uchar speed[6][32];
    int ret = 0;
    ret = ReadRawSpeedFile(from, (uchar*)speed, sizeof(speed));
    if(ret)
    {
        ret = MH_FILE_ERROR_READ;
    }
    else
    {
        ret = WriteRawSpeedFile(to, (uchar*)speed, sizeof(speed));
        if(ret)
        {
            ret = MH_FILE_ERROR_WRITE;
        }
    }
    return ret;
}
/*
 * copy yarn stop between file
 */
int CopyStopFile(char *from, char *to)
{
    struct
    {
        ushort stop1[8][2][8]; //1-8
        ushort stop9[8][2][8]; //9-16
        ushort gout[16][2];
    } stop;
    int ret = 0;
    ret = ReadRawSpeedFile(from, (uchar*)&stop, sizeof(stop));
    if(ret)
    {
        ret = MH_FILE_ERROR_READ;
    }
    else
    {
        ret = WriteRawSpeedFile(to, (uchar*)&stop, sizeof(stop));
        if(ret)
        {
            ret = MH_FILE_ERROR_WRITE;
        }
    }
    return ret;
}
/*
 * @translator: 转换器
 * @pat_fn:花样文件名
 * @type:0-pat, 1-dat
 * @ret:TRUE-成功，FALSE-失败
 */
void ConvertPat2Dat(TRANSLATOR *translator, uchar *data, int width, int height)
{
    translator->width = width;
    translator->height = height;
    translator->pat_buf = (uchar*)malloc (width * height);

    int h, w;
    int span = (width + 1) / 2;
    for(h = 0; h < height; ++h)
    {
        for(w = 0; w < width; ++w)
        {
            uchar c = data[span * h + w / 2];
            c = w & 1 ? c & 0xF : c >> 4;
            translator->pat_buf[h * width + w] = c;
        }
    }
}
/*
 * copy control page between file for different format
 */
int CopyControlFile(char *from, char *to)
{
    int ret = 0;
    KNIT_PAGE head;
    KNIT_PAGE *con = NULL;
    ushort height;
    int len = strlen(from);
    enum {
        CONTROL_NONE = 0,
        CONTROL_CON = 1,
        CONTROL_CNT = 2,
    }con_type = CONTROL_NONE;
    if(len >= sizeof(".con"))
    {
        char *p = &from[len - sizeof(".con") + 1];
        if(!strcmp(p, ".con"))
        {
            con_type = CONTROL_CON;
        }
        else if(!strcmp(p, ".CNT"))
        {
            con_type = CONTROL_CNT;
        }
    }
    switch(con_type)
    {
    case CONTROL_NONE:
        break;
    case CONTROL_CON:
        ret = ReadRawConFile(from, &height, &con, &head);
        break;
    case CONTROL_CNT:
        {
            ret = ReadRawConFile(from, &height, &con, &head);
    /*
          ushort dat_head[8] = {0};  // **
            uchar *data = NULL;
            char dat[MH_FILE_NAME_MAX_LEN] = {0};
            memcpy(dat, to, strlen(to) - 4);
            strcat(dat, ".PAT");
            ret = ReadRawPatFile(dat, dat_head, &data);
            if(ret)
            {
                FREE(data);
            }
            else
            {
                TRANSLATOR *translator = (TRANSLATOR*)calloc(1, sizeof(TRANSLATOR));
                ConvertPat2Dat(translator, data, dat_head[0], dat_head[1]);
                FREE(data);

                //初始化纱嘴停放点和方向
                for(i = 0; i < GAUZE_NUM; i++)
                {
                    translator->io[i] = 2;
                    translator->gauze_pos[i] = -1;
                    translator->gauze_dir[i] = -1;
                }
                KNIT_PAGE *src = NULL;
                KNIT_PAGE *des = NULL;
                ushort height = 0;
                ret = ReadRawConFile(from, &height, &src, &head);
                if(ret)
                {
                    FREE(src);
                    FREE(translator->pat_buf);
                    FREE(translator);
                    break;
                }

                memcpy(dat, from, strlen(from) - 4);
                strcat(dat, ".prm");
                translator->prm_buf = calloc(1, 512);
                ReadRawFile(dat, translator->prm_buf, 512);

                des = calloc(height, sizeof(KNIT_PAGE));
                for(i = 0; i < height; ++i)
                {
                    uchar *pcon_buf = (uchar*)&des[i];
                    uchar *pcnt_buf = (uchar*)&src[i];

                    translator_exec(translator, (i + 1), pcon_buf, pcnt_buf);
                }

                translator_iterate_loop (translator, (uchar*)des);
                FREE(src);
                FREE(des);
                FREE(translator->pat_buf);
                FREE(translator->prm_buf);
                FREE(translator);
            }//*/
        }
        break;
    }
    if(ret)
    {
        ret = MH_FILE_ERROR_READ;
    }
    else
    {
        ret = WriteRawConFile(to, height, con, &head);
        if(ret)
        {
            ret = MH_FILE_ERROR_WRITE;
        }
    }
    FREE(con);
    return ret;
}
/*
 * copy flower data between file for different format
 */
int CopyFlowerFile(char *from, char *to)
{
    int ret = 0;
    int size = 0;
    uchar *buf = NULL;
    int len = strlen(from);
    enum {
        FLOWER_NONE = 0,
        FLOWER_DAT = 1,
        FLOWER_PAT = 2,
    } flw_type = FLOWER_NONE;
    if(len >= sizeof(".dat"))
    {
        char *p = &from[len - sizeof(".dat") + 1];
        if(!strcmp(p, ".dat"))
        {
            flw_type = FLOWER_DAT;
        }
        else if(!strcmp(p, ".PAT"))
        {
            flw_type = FLOWER_PAT;
        }
    }
    switch(flw_type)
    {
    case FLOWER_NONE:
        break;
    case FLOWER_DAT:
    case FLOWER_PAT:
        {
            struct stat statbuff;
            if(!stat(from, &statbuff))
            {
                size = statbuff.st_size;
            }
            if(size)
            {
                buf = calloc(1, size);
                ret = ReadRawFile(from, buf, size);
            }
            else
            {
                ret = MH_FILE_ERROR_READ;
            }
        }
        break;
    }
    if(ret)
    {
        ret = MH_FILE_ERROR_READ;
    }
    else
    {
        ret = WriteRawFile(to, buf, size);
        if(ret)
        {
            ret = MH_FILE_ERROR_WRITE;
        }
    }
    FREE(buf);
    return ret;
}
/*
 * copy system param between file
 */
int CopySysParamFile(char *from, char *to)
{
    int ret = 0;
    return ret;
}



